package docker

import (
	"fmt"
	"math/rand"
	"pdocker/config"
	"time"

	"github.com/docker/docker/api/types"
)

// Container API

func GetRandomString() string {
	str := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
	b := []byte(str)
	var result []byte
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	for i := 0; i < config.Config.Docker.DefaultRandomStringLength; i++ {
		result = append(result, b[r.Intn(len(b))])
	}
	if len(result) > 1 && result[0] == byte(0) {
		return GetRandomString()
	}
	return string(result)
}

func GetPublicPort(p uint16, container *types.Container) (uint16, error) {
	for _, port := range container.Ports {
		if port.PrivatePort == p {
			return port.PublicPort, nil
		}
	}
	return 0, fmt.Errorf("Cannot found public port ")
}

//
//func LabLoader(all bool, filters []string, force bool, callback string) error {
//	if all {
//		folders, err := ioutil.ReadDir(config.Config.Docker.LabDir)
//		if err != nil {
//			return err
//		} else {
//			var folderList []string
//			for _, folder := range folders {
//				folderList = append(folderList, folder.Name())
//			}
//			go func() {
//				for _, folder := range folderList {
//					loadImage(folder, force, callback)
//					Log.Printf("%s finish", folder)
//				}
//			}()
//			return nil
//		}
//	} else {
//		for _, filter := range filters {
//			go func() {
//				loadImage(filter, force, callback)
//				Log.Printf("%s finish", filter)
//			}()
//		}
//	}
//	return nil
//}
